package HW.hw5;

/**************************************************************
* Class JumpIt
* @author Jeff Thomas - Thomas007
* @vesion 1.0.0
* Date: 04.02.2008
* Copyright 2008, Jeff Thomas
***************************************************************/
import java.io.*;
import java.util.*;

public class JumpIt 
{
	public static void main(String[] args)
	{	
		readData();
	}
/**************************************************************
* jumpIt
* @return - Integer value of cost to get to other side of board
* @param A - Array of integers representing the jumpIt board
* Task:	1. Wrapper method for jumpIt
* Side Effects: None
***************************************************************/	
	public static int jumpIt(int[] A)
	{
		return jumpIt(0, A);
	}
/**************************************************************
* jumpIt
* @return - Integer value of cost to get to other side of board
* @param A - Array of integers representing the jumpIt board
* @param start - Integer value of position on game board
* @param lastCell - Integer value of position at end of board
* Task:	1. Use 3 base cases where there is a limited number of
* 			cells and determine the best move to make.
*			2. Perform recursion for all other cases.
* Side Effects: None
***************************************************************/
	public static int jumpIt(int start, int[] A)
	{
	   int arraySize = A.length - start;
		
		int cost = 0;
		
		if (arraySize == 1)
		{
			cost = A[A.length - 1];
		}
		else if (arraySize == 2)
		{	
			cost = A[A.length - 2] + A[A.length -1];
		}
		else if (arraySize == 3)
		{
			cost = A[start] + A[A.length -1];
		}
		else
		{
			cost = A[start] + Math.min(jumpIt(start + 1, A), jumpIt(start + 2, A));
		}
		return cost;
	}
/**************************************************************
* Read Data
* Task:	1. Read each game board from input.dat
*			2. Create an integer array for each game board
*			3. Pass each array to jumpIt and print the result
* Side Effects: None
***************************************************************/
	public static void readData()
	{
		try 
		{
			BufferedReader br = new BufferedReader(new FileReader("input.dat"));
			String line;
			try
			{
				while ((line = br.readLine()) != null)
				{
					StringTokenizer st = new StringTokenizer(line);
					int[] board = new int[Integer.parseInt(st.nextToken())];
					for (int i = 0; i < board.length; i++)
					{
						board[i] = Integer.parseInt(st.nextToken());
					}
					System.out.println(jumpIt(board));
				}
			}
			catch (IOException e)
			{
				System.err.println("IOException: " + e.getMessage());
			}
		}
		catch (FileNotFoundException e)
		{
			System.err.println("FileNotFoundException: " + e.getMessage());
		}
	}
}
/**************************************************************
* I, Jeff Thomas, certify that each part of this program was
* written by me, except as noted within the code.
*
*						_________________________
***************************************************************/